package com.letao.server.controller;

import com.letao.server.dto.Result;
import com.letao.server.dto.UserInfoDTO;
import com.letao.server.entity.OrderInfo;
import com.letao.server.entity.SeckillInfo;
import com.letao.server.entity.UserAddress;
import com.letao.server.service.IOrderDetailSeckillService;
import com.letao.server.service.ISeckillService;
import com.letao.server.service.IUserAddressService;
import com.letao.server.utils.UserHolderUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/seckill")
@Slf4j
public class SecKillController {
    @Resource
    ISeckillService seckillService;

    @Resource
    IOrderDetailSeckillService orderDetailSeckillService;

    @Resource
    private IUserAddressService userAddressService;

    @PostMapping
    private Result addSeckill(@RequestBody SeckillInfo info) {
        if (info.getPrice() == null || info.getConstPrice() == null || info.getEndTime() == null
                || info.getBeginTime() == null || info.getNum() == null || info.getName() == null
                || info.getDescript() == null)
            return Result.fail("数据不完整");
        info.setId(null);
        info.setStock(info.getNum());
        info.setModifiedTime(null);
        info.setCreateTime(null);
        seckillService.save(info);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    private Result removeSeckill(@PathVariable Integer id) {
        SeckillInfo one = seckillService.getById(id);
        if (one == null) return Result.fail("依赖不存在");
        if (one.getBit() == 0) {
            return Result.fail("重复删除");
        }
        one.setBit(0);
        seckillService.updateById(one);
        return Result.success();
    }

    @GetMapping
    private Result getSeckill(@RequestParam(defaultValue = "1") Long page,
                              @RequestParam(defaultValue = "10") Long pageSize) {
        return seckillService.getPage(page, pageSize);
    }

    @PostMapping("/order/{id}")
    private Result seckillOrder(@PathVariable Long id, @RequestBody OrderInfo orderInfo) {
        UserInfoDTO user = UserHolderUtils.getUser();
        UserAddress userAddress = userAddressService.query().eq("user_id", user.getId()).one();

        if (user.getId() == null || orderInfo.getPaymentMethod() == null || orderInfo.getDeliveryMethod() == null) {
            return Result.fail("数据不完整");
        }

        if (orderInfo.getAddresseeName() == null){
            if (userAddress == null) {
                return Result.fail("用户无地址");
            }
            orderInfo.setAddresseeName(userAddress.getConsigneeName());
        }
        if (orderInfo.getAddresseePhoneNumber() == null)
            orderInfo.setAddresseePhoneNumber(userAddress.getConsigneePhoneNumber());
        if (orderInfo.getAddresseeSex() == null)
            orderInfo.setAddresseeSex(userAddress.getConsigneeSex());

        if (orderInfo.getProvince() == null)
            orderInfo.setProvince(userAddress.getProvinceId());
        if (orderInfo.getCity() == null)
            orderInfo.setCity(userAddress.getCityId());
        if (orderInfo.getDistrict() == null)
            orderInfo.setDistrict(userAddress.getDistrictId());
        if (orderInfo.getDetail() == null)
            orderInfo.setDetail(userAddress.getDetail());

        orderInfo.setUserId(user.getId());
        orderInfo.setOrderStatus(1);

        return seckillService.seckillOrder(id, orderInfo);
    }

    @GetMapping("/order/{id}")
    private Result getOrderDeatil(@PathVariable Long id) {

        return orderDetailSeckillService.getInfo(id, null);
    }

    @GetMapping("/order/user/{id}")
    private Result getOrderDeatilUser(@PathVariable Long id) {
        UserInfoDTO user = UserHolderUtils.getUser();
        return orderDetailSeckillService.getInfo(id, user.getId());
    }

    @GetMapping("/{id}")
    private Result queryOne(@PathVariable Long id) {
        return Result.success(seckillService.getById(id));
    }

    @GetMapping("/admin")
    private Result queryAdmin(@RequestParam(required = false) Long id,
                              @RequestParam(required = false) String name,
                              @RequestParam(required = false) Boolean hasStock,
                              @RequestParam(required = false) Integer seckillState,
                              @RequestParam(defaultValue = "1") Long page,
                              @RequestParam(defaultValue = "10") Long pageSize) {
        return seckillService.getAdminPage(id, name, hasStock, seckillState, page, pageSize);

    }

    @PutMapping
    private Result setSeckill(@RequestBody SeckillInfo info) {
        if (info.getId() == null) {
            return Result.fail("无此id对应商品");
        }
        SeckillInfo seckillInfo = seckillService.getById(info.getId());
        if (seckillInfo.getBit() == 0) {
            return Result.fail("商品已删除");
        }
        info.setModifiedTime(null);
        info.setCreateTime(null);
        log.debug("11111111111111{}", info.getNum());
        if (info.getNum() != null) {
            Integer change = info.getNum() - seckillInfo.getNum();
            log.debug("222222222{}", change);
            seckillInfo.setStock(seckillInfo.getStock() + change);
            seckillService.updateById(seckillInfo);
        }

        seckillService.updateById(info);

        log.info("管理员修改了id={}的秒杀商品信息", info.getId());
        return Result.success();
    }

}
